##  Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 
##
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os
import sys
import shutil
sys.path.append(os.path.join(os.getcwd(),'triangulum_dev','python'))

import pytriangulum
import pytriangulum.utils
from  pytriangulum.utils import inno

def MKDIR(d):
    try:
        os.mkdir(d)
    except Exception, e:
        pass
        #print e
    

def getVersion():
    fh = open('VERSION')
    ver = fh.readlines()[2].split(':')[1].replace('\n','')
    fh.close()
    return ver


def build():
    src_dir = 'code'
    dst_dir = '{pf}/Triangulum/Source/PY/VERSION_'+getVersion()
    app = "pytriangulum-"+getVersion()+".win32"
    version = getVersion()
    output_iss = 'pyTriangulumBaseSource.iss'
    
    try:
        os.remove('Output\\'+"PyTriangulum-"+getVersion()+".win32"+'.exe')
    except Exception,e :
        pass
        #print e

    if os.path.isfile('Output\\setup.exe'):
        os.rename('Output\\setup.exe','Output\\'+"PyTriangulumSource-"+getVersion()+".win32"+'.exe')
        try:  
            shutil.rmtree('code')
        except Exception,e :
            print e
        return False
    else:
        try:  
            shutil.rmtree('code')
        except Exception,e :
            pass
            #print e
        MKDIR('code')
        MKDIR('code/src')
        MKDIR('code/src/dev')
        MKDIR('code/src/utils')
        os.system('copy \"src\\*.py\" \"code\\src\"')
        os.system('copy \"src\\dev\\*.py\" \"code\\src\\dev\"')
        os.system('copy \"src\\utils\\*.py\" \"code\\src\\utils\"')
        shutil.copy('VERSION','code\\VERSION')
        shutil.copy('bdist_wininstaller.bat','code\\bdist_wininstaller.bat')
        shutil.copy('LICENSE','code\\LICENSE')
        shutil.copy('setup.py','code\\setup.py')
        shutil.copy('inno_script.py','code\\inno_script.py')

        inno.genInnoScript(output_iss = output_iss,
                      src_dir = src_dir,
                      dst_dir = dst_dir,
                      app_name = app,
                      version = version)
    return True
#=============================================

def main():
   if build():
       pass
       os.system("Compil32.exe /cc  pyTriangulumBaseSource.iss")
##
###=============================================
##
if __name__ == '__main__':
    main()
